package com.example.mall.controller;

import com.example.mall.common.api.CommonPage;
import com.example.mall.common.api.CommonResult;
import com.example.mall.dto.SupplierDto;
import com.example.mall.dto.UserDto;
import com.example.mall.mbg.model.Role;
import com.example.mall.mbg.model.Supplier;
import com.example.mall.mbg.model.User;
import com.example.mall.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * SupplierController
 * Created by on 2023/12/27.
 */
@Api(tags = "SupplierController", description = "供应商管理")
@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierController.class);

    @ApiOperation("画面：供应商列表")
    @RequestMapping(value = {"init"},method = {RequestMethod.GET})
    public String init(Model model, HttpSession session) {
        List<SupplierDto> supplierDtos = new ArrayList<>();
        List<Supplier> suppliers = supplierService.listAll();
        suppliers.forEach(supplier -> {
            SupplierDto supplierDto = new SupplierDto(supplier);
            supplierDtos.add(supplierDto);
        });
        model.addAttribute("supplierDtos", supplierDtos);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "supplier-list";
    }

    @ApiOperation("画面：供应商录入")
    @RequestMapping(value = {"detail"},method = {RequestMethod.GET})
    public String detail(Model model, HttpSession session) {
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "supplier-detail";
    }

    @ApiOperation("获取所有供应商列表")
    @RequestMapping(value = "listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Supplier>> getSupplierList() {
        return CommonResult.success(supplierService.listAll());
    }

    @ApiOperation("添加供应商")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createSupplier(@RequestBody Supplier supplier) {
        CommonResult commonResult;
        supplier.setSupplierid(supplierService.getNextId());
        supplier.setCreatetime(new Date());
        supplier.setUpdatetime(supplier.getCreatetime());
        int count = supplierService.create(supplier);
        if (count == 1) {
            commonResult = CommonResult.success(supplier);
            LOGGER.debug("createSupplier success:{}", supplier);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createSupplier failed:{}", supplier);
        }
        return commonResult;
    }

    @ApiOperation("画面：供应商编辑")
    @RequestMapping(value = {"edit"},method = {RequestMethod.GET})
    public String edit(Model model, Integer supplierid, HttpSession session) {
        Supplier supplier = supplierService.get(supplierid);
        SupplierDto supplierDto = new SupplierDto(supplier);
        model.addAttribute("supplier", supplierDto);
        Object user1 = session.getAttribute("user");
        if (user1 != null) {
            model.addAttribute("user", user1);
        }
        return "supplier-detail";
    }

    @ApiOperation("更新指定id供应商信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateSupplier(@PathVariable("id") Integer supplierid, @RequestBody Supplier supplier, BindingResult bindingResult) {
        CommonResult commonResult;
        int count = supplierService.update(supplierid, supplier);
        if (count == 1) {
            commonResult = CommonResult.success(supplier);
            LOGGER.debug("updateSupplier success:{}", supplier);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateSupplier failed:{}", supplier);
        }
        return commonResult;
    }

    @ApiOperation("删除指定id的供应商")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteSupplier(@PathVariable("id") Integer supplierid) {
        CommonResult commonResult;
        int count = supplierService.delete(supplierid);
        if (count == 1) {
            commonResult = CommonResult.success(null);
            LOGGER.debug("deleteSupplier success:id={}", supplierid);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("deleteSupplier failed:id={}", supplierid);
        }
        return commonResult;
    }

    @ApiOperation("分页查询供应商列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Supplier>> listSupplier(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        List<Supplier> supplierList = supplierService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(supplierList));
    }

    @ApiOperation("获取指定id的供应商详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Supplier> supplier(@PathVariable("id") Integer supplierid) {
        return CommonResult.success(supplierService.get(supplierid));
    }
}
